import React  from 'react';
import {
	Form,
	Input, 
	message,
	Modal,
} from 'antd';
import { addModelInput } from 'src/services/DataManage/type';
import { createModel } from 'src/services/DataManage';

interface Iprops {
    createFileVisible:boolean
    modalVisible:any
    getModelFiles:any
	isUpdate:any
	fileData:any
}

// eslint-disable-next-line react/display-name
export const CreateFile = React.forwardRef ( ( props: Iprops, formRef: any ) => {

	const [ form ] = Form.useForm();

	const handleCancle = () => {
		props.modalVisible( false );
		form.resetFields();
	};
	
	
	const handleOk = () => {
	   form.validateFields().then( value => {
	        handleCreate( value );
			handleCancle();
		} ).catch( info => {
			message.warning( '请完善信息' );
		} ); 
	};

	//新增文件夹/更新文件夹
	const handleCreate = async ( data: addModelInput ) => {
		let saveInfo = {};
		if( props.isUpdate ) {
			saveInfo = { 'id': props.fileData.id,
				'cronExpression': '',...data };
		}else {
			saveInfo = { 'cronExpression': '', 'isFile': 1, 'parentCode': 0, 'position': 0 ,...data };
		}
		const result = await createModel( { data: saveInfo } );
		if ( result.data && result.data.success ) {
			if( props.isUpdate ) {
				message.success( '更新文件夹成功' );
			}else {
				message.success( '新建文件夹成功' );
			}
			//请求最新的文件数
			props.getModelFiles();
		}
	};
	

	return (
		<Modal okText={'确定'} 
			visible={props.createFileVisible}
			cancelText={'取消'} 
			onCancel={handleCancle}
			onOk={handleOk}
			title={!props.isUpdate?'添加文件夹':'更新文件夹'}>
			<Form form={form} ref={ formRef } labelCol={{ span: 3 }}>
				<Form.Item label={'名称'}
					rules={[ { required: true, message: '名称不能为空' } ]}
					name={'name'}>
					<Input/>
				</Form.Item>
				<Form.Item label={'描述'}
						   name={'comments'}>
					<Input/>
				</Form.Item>
			</Form>
		</Modal>
	);
} );